﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.Project.Entity;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Course.Entity;

public partial class Theme_3_OpenHistory : System.Web.UI.Page
{
    Course opCourse = new Course();
    Resource opResource = new Resource();
    ProjectPlan1 opProjectPlan1 = new ProjectPlan1();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            ViewState["ProjectID"] = "0";
            if (Request.QueryString["ProjectID"] != null)
            {
                string projectid = Request.QueryString["ProjectID"].ToString();                         //方案ID，如果没有方案则为0
                ViewState["ProjectID"] = projectid;
            }
            //VOTTrainProject voProject = opProject.LoadTrainProject(int.Parse(ViewState["ProjectID"].ToString()));

            ViewState["ProjectClassID"] = "0";
            if (Request.QueryString["ProjectClassID"] != null)
            {
                string projectclassid = Request.QueryString["ProjectClassID"].ToString();               //班级ID，如果没有则为0
                ViewState["ProjectClassID"] = projectclassid;
            }

            //VOTTrainClass voProjectClass = opProjectUser.LoadProjectClass(int.Parse(ViewState["ProjectClassID"].ToString()));
            //必须有，该用户参数，必须用
            string projectclassuserid = Request.QueryString["ProjectClassUserID"].ToString();       //用户ID
            ViewState["ProjectClassUserID"] = projectclassuserid;
            ViewState["ProjectResourceID"] = "0";
            if (Request.QueryString["ProjectResourceID"] != null)
            {
                string projectresourceid = Request.QueryString["ProjectResourceID"].ToString();         //方案资源ID，如果没有则为0
                ViewState["ProjectResourceID"] = projectresourceid;
            }

            //VOTProjectResource voProjectResource = opProjectResource.LoadProjectResource(int.Parse(ViewState["ProjectResourceID"].ToString()));
            //课程资源ID,必须有该参数，
            ViewState["CourseResourceID"] = "0";
            string courseresourceid = Request.QueryString["CourseResourceID"].ToString();
            ViewState["CourseResourceID"] = courseresourceid;

            //必须有该参数，课程资源信息ID
            ViewState["ResourceID"] = "0";
            string projectresourceresourceid = Request.QueryString["ResourceID"].ToString();
            ViewState["ResourceID"] = projectresourceresourceid;

            ViewState["SscrlogCurrentPosition"] = "0";
            if (Request.QueryString["SscrlogCurrentPosition"] != null)
            {
                string currentposition = Request.QueryString["SscrlogCurrentPosition"].ToString();
                ViewState["SscrlogCurrentPosition"] = currentposition;
            }
            //有可能是试卷，有可能是课件资源
            VOTCourseResource voCourseResource = opCourse.LoadCourseResource(int.Parse(ViewState["CourseResourceID"].ToString()));

            string jydTime = Request.QueryString["jydTime"].ToString();   //竞业达传递的时间记录
            ViewState["jydTime"] = jydTime;

            //必须有该参数
            string projectresourceresourcestarttime = Request.QueryString["ProjectResourceResourceStartTime"].ToString();   //开始学习时间
            ViewState["ProjectResourceResourceStartTime"] = projectresourceresourcestarttime;

            VOTStudentStudyCouReslog vo = new VOTStudentStudyCouReslog();
            vo.FKSscrlogTrainproID = int.Parse(ViewState["ProjectID"].ToString());
            vo.FKSscrlogTrainclaID = int.Parse(ViewState["ProjectClassID"].ToString());
            vo.FKSscrlogUserID = int.Parse(ViewState["ProjectClassUserID"].ToString());
            vo.FKSscrlogProresID = int.Parse(ViewState["ProjectResourceID"].ToString());
            vo.SscrlogStartTime = DateTime.Parse(ViewState["ProjectResourceResourceStartTime"].ToString());
            vo.SscrlogEndTime = DateTime.Now;
            vo.SscrlogCurrentPosition = double.Parse(ViewState["SscrlogCurrentPosition"].ToString());

            //资源信息
            vo.FKSscrlogCourseID = voCourseResource.FKCourseresCourseID;
            vo.FKSscrlogCourseresID = voCourseResource.CourseresID;
            vo.FKSscrlogPaperID = voCourseResource.FKCoursersPaperID;
            vo.FKSscrlogResourceID = voCourseResource.FKCoursersResourceID;
            vo.SscrlogClass = 1;
            vo.BrowseIP = PowerCreator.LMS.Common.Utils.IPUtils.GetIP;

            //是否需要更新进度
            string time = PowerCreator.LMS.Common.Utils.TimeUtils.TimeCompart(vo.SscrlogStartTime, vo.SscrlogEndTime).TotalSeconds.ToString();
            long vtime = PowerCreator.LMS.Common.Utils.TimeUtils.GetTimeQian(time);
            vo.SscrlogTotalTime = vtime;
            
           

            if (voCourseResource.CoursersClass == Convert.ToInt32(CoursersClass.Courseware))
            {
                VOTResource voResource = opCourse.LoadResource(vo.FKSscrlogResourceID);
                if (voResource.ResourceClass != Convert.ToInt32(SysResourceType.PowerCreator) && voResource.ResourceClass != Convert.ToInt32(SysResourceType.Video))
                {
                    vtime = PowerCreator.LMS.Web.Config.WebConfig.MinStudyTime + 1;
                }
            }
	    vtime =1111;
	    if (ViewState["jydTime"] != null)
            {
                Regex r = new Regex(@"^\d+$");
                string strTemp=ViewState["jydTime"].ToString();
                if (r.IsMatch(strTemp))
                {
                    vtime = long.Parse(strTemp);
                }
            }
            opProjectPlan1.AddProjectResourceResource(vo);

            if (vtime > PowerCreator.LMS.Web.Config.WebConfig.MinStudyTime)
            {
                ProjectPlanInfo info = new ProjectPlanInfo();
                info.ProjectID = int.Parse(ViewState["ProjectID"].ToString());
                info.ClassID = int.Parse(ViewState["ProjectClassID"].ToString());
                info.UserID = int.Parse(ViewState["ProjectClassUserID"].ToString());
                int projectresourceid = int.Parse(ViewState["ProjectResourceID"].ToString());
                if (opProjectPlan1.CheckProjectOverdueTime(info))
                {
                    if (opProjectPlan1.CheckProjectPlanCourseResource(info, voCourseResource.CourseresID, vo.FKSscrlogResourceID))
                    {
                        if (opProjectPlan1.CheckProjectPlanProjectResource(info, vo.FKSscrlogProresID))
                        {
                            opProjectPlan1.CheckProjectPlan(info);
                        }
                    }
                }
            }
        }
    }
}
